package seachdir;

import java.io.File;
import java.util.Scanner;

public class SeachDir {
	private static String d = null;
	private static String s=null;
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入要查找的盘符(如：c盘输入c):");
		d = in.nextLine();
		System.out.println("请输入你要查找的文件名称：");
		s=in.nextLine();
        
		int i=0;
		File[] fs = File.listRoots();
		switch(d){
		case "c":i=0;break;
		case "d":i=1;break;
		case "e":i=2;break;
		case "f":i=3;break;
		case "g":i=4;break;
		}
		
		System.out.println("您要找的文件为:" + fs[i] + s);
		System.out.println("开始查找文件...");
		File[] f = fs[i].listFiles();
		for (int j = 0; j < f.length; j++) {
			if (f[j] != null) {
				if (f[j].getName().toString().equals(s)) {
					System.out.println("搜索到的文件为：" + f[j].getPath().toString());
				} else {
					seacDir(f[j]);
				}
			}
		}
		
		System.out.println("查找结束！");
		in.close();
	}

	private static void seacDir(File file) {
		File[] fs = file.listFiles();
		if (fs != null) {
			for (int i = 0; i < fs.length; i++) {
				if (file != null) {
					if (fs[i].isDirectory()) {
						seacDir(fs[i]);
					} else {
						if (fs[i].getName().toString().equals(s)) {
							System.out.println(fs[i].getPath().toString());
						} 
					}
				}
			}
		}
	}
}
